using System;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Collections.Generic;
using System.Reflection;

namespace DocumentManagement
{
  /// <summary>
  /// Tsetup Data Object Class
  /// </summary>
  public partial class DOTsetup
  {
    #region Class Constant declarations
    		/// <summary>
		/// The Table Column for the Tsetup PICKLIMIT
		/// </summary>
		public const string RowPICKLIMIT = "PICKLIMIT";

		/// <summary>
		/// The Table Column for the Tsetup MAXDAYS
		/// </summary>
		public const string RowMAXDAYS = "MAXDAYS";

		/// <summary>
		/// The Table Column for the Tsetup MAXREQ
		/// </summary>
		public const string RowMAXREQ = "MAXREQ";

		/// <summary>
		/// The Table Column for the Tsetup SENDMAIL
		/// </summary>
		public const string RowSENDMAIL = "SENDMAIL";

		/// <summary>
		/// The Table Column for the Tsetup SMTPIP
		/// </summary>
		public const string RowSMTPIP = "SMTPIP";

		/// <summary>
		/// The Table Column for the Tsetup SENDEREMAIL
		/// </summary>
		public const string RowSENDEREMAIL = "SENDEREMAIL";

		/// <summary>
		/// The Table Column for the Tsetup SENDERNAME
		/// </summary>
		public const string RowSENDERNAME = "SENDERNAME";

		/// <summary>
		/// The Table Column for the Tsetup SENDSTSERR
		/// </summary>
		public const string RowSENDSTSERR = "SENDSTSERR";

		/// <summary>
		/// The Table Column for the Tsetup SENDSTSTO
		/// </summary>
		public const string RowSENDSTSTO = "SENDSTSTO";

		/// <summary>
		/// The Table Column for the Tsetup RETWARN
		/// </summary>
		public const string RowRETWARN = "RETWARN";

		/// <summary>
		/// The Table Column for the Tsetup PICKFOLDER
		/// </summary>
		public const string RowPICKFOLDER = "PICKFOLDER";

		/// <summary>
		/// The Table Column for the Tsetup QCLETBODY
		/// </summary>
		public const string RowQCLETBODY = "QCLETBODY";

		/// <summary>
		/// The Table Column for the Tsetup ORDERNO
		/// </summary>
		public const string RowORDERNO = "ORDERNO";

		/// <summary>
		/// The Table Column for the Tsetup ORDERTOEMAIL
		/// </summary>
		public const string RowORDERTOEMAIL = "ORDERTOEMAIL";


#region Stored Procedures
private const string m_StoredProcedureConstCreate = "spTsetup_Create";
private const string m_StoredProcedureConstDelete = "spTsetup_Delete";
private const string m_StoredProcedureConstRetrieve = "spTsetup_Retrieve";
private const string m_StoredProcedureConstRetrieveall = "spTsetup_Retrieveall";
private const string m_StoredProcedureConstUpdate = "spTsetup_Update";
#endregion
    
    
    #endregion

    #region Class Variable declarations
    	#region DO Variables
		private decimal m_PICKLIMIT;
		private decimal m_MAXDAYS;
		private decimal m_MAXREQ;
		private bool m_SENDMAIL;
		private string m_SMTPIP;
		private string m_SENDEREMAIL;
		private string m_SENDERNAME;
		private bool m_SENDSTSERR;
		private string m_SENDSTSTO;
		private bool m_RETWARN;
		private decimal m_PICKFOLDER;
		private string m_QCLETBODY;
		private int m_ORDERNO;
		private string m_ORDERTOEMAIL;
	#endregion
    
	#region Default Variables
	private SQLDataObject sqlData;
	private List<DOTsetup> m_Tsetups = new List<DOTsetup>();
	#endregion
	
    #endregion

    #region Class Constructors & Destructors
    /// <summary>
    /// Class Constructor
    /// </summary>
    public DOTsetup() { }
    #endregion

    #region Class Properties
    #region DO Properties
		/// <summary>
		/// PICKLIMIT
		/// </summary>
		/// <remarks>
		/// This property contains the Tsetup PICKLIMIT
		/// </remarks>
		public decimal PICKLIMIT
		{
			get { return (this.m_PICKLIMIT); }
			set { this.m_PICKLIMIT = value; }
		}

		/// <summary>
		/// MAXDAYS
		/// </summary>
		/// <remarks>
		/// This property contains the Tsetup MAXDAYS
		/// </remarks>
		public decimal MAXDAYS
		{
			get { return (this.m_MAXDAYS); }
			set { this.m_MAXDAYS = value; }
		}

		/// <summary>
		/// MAXREQ
		/// </summary>
		/// <remarks>
		/// This property contains the Tsetup MAXREQ
		/// </remarks>
		public decimal MAXREQ
		{
			get { return (this.m_MAXREQ); }
			set { this.m_MAXREQ = value; }
		}

		/// <summary>
		/// SENDMAIL
		/// </summary>
		/// <remarks>
		/// This property contains the Tsetup SENDMAIL
		/// </remarks>
		public bool SENDMAIL
		{
			get { return (this.m_SENDMAIL); }
			set { this.m_SENDMAIL = value; }
		}

		/// <summary>
		/// SMTPIP
		/// </summary>
		/// <remarks>
		/// This property contains the Tsetup SMTPIP
		/// </remarks>
		public string SMTPIP
		{
			get { return (this.m_SMTPIP); }
			set { this.m_SMTPIP = value; }
		}

		/// <summary>
		/// SENDEREMAIL
		/// </summary>
		/// <remarks>
		/// This property contains the Tsetup SENDEREMAIL
		/// </remarks>
		public string SENDEREMAIL
		{
			get { return (this.m_SENDEREMAIL); }
			set { this.m_SENDEREMAIL = value; }
		}

		/// <summary>
		/// SENDERNAME
		/// </summary>
		/// <remarks>
		/// This property contains the Tsetup SENDERNAME
		/// </remarks>
		public string SENDERNAME
		{
			get { return (this.m_SENDERNAME); }
			set { this.m_SENDERNAME = value; }
		}

		/// <summary>
		/// SENDSTSERR
		/// </summary>
		/// <remarks>
		/// This property contains the Tsetup SENDSTSERR
		/// </remarks>
		public bool SENDSTSERR
		{
			get { return (this.m_SENDSTSERR); }
			set { this.m_SENDSTSERR = value; }
		}

		/// <summary>
		/// SENDSTSTO
		/// </summary>
		/// <remarks>
		/// This property contains the Tsetup SENDSTSTO
		/// </remarks>
		public string SENDSTSTO
		{
			get { return (this.m_SENDSTSTO); }
			set { this.m_SENDSTSTO = value; }
		}

		/// <summary>
		/// RETWARN
		/// </summary>
		/// <remarks>
		/// This property contains the Tsetup RETWARN
		/// </remarks>
		public bool RETWARN
		{
			get { return (this.m_RETWARN); }
			set { this.m_RETWARN = value; }
		}

		/// <summary>
		/// PICKFOLDER
		/// </summary>
		/// <remarks>
		/// This property contains the Tsetup PICKFOLDER
		/// </remarks>
		public decimal PICKFOLDER
		{
			get { return (this.m_PICKFOLDER); }
			set { this.m_PICKFOLDER = value; }
		}

		/// <summary>
		/// QCLETBODY
		/// </summary>
		/// <remarks>
		/// This property contains the Tsetup QCLETBODY
		/// </remarks>
		public string QCLETBODY
		{
			get { return (this.m_QCLETBODY); }
			set { this.m_QCLETBODY = value; }
		}

		/// <summary>
		/// ORDERNO
		/// </summary>
		/// <remarks>
		/// This property contains the Tsetup ORDERNO
		/// </remarks>
		public int ORDERNO
		{
			get { return (this.m_ORDERNO); }
			set { this.m_ORDERNO = value; }
		}

		/// <summary>
		/// ORDERTOEMAIL
		/// </summary>
		/// <remarks>
		/// This property contains the Tsetup ORDERTOEMAIL
		/// </remarks>
		public string ORDERTOEMAIL
		{
			get { return (this.m_ORDERTOEMAIL); }
			set { this.m_ORDERTOEMAIL = value; }
		}
		#endregion
    
	#region Default Properties
	public List<DOTsetup> Tsetups
	{
	  get { return m_Tsetups; }
	  set { m_Tsetups = value; }
	}    
	#endregion
	
    #endregion

    #region Class Methods
    public DataSet Retrieve()
    {
      DataSet returnDS = new DataSet();
      sqlData = new SQLDataObject();
      sqlData.ExecuteDataSetSQL(m_StoredProcedureConstRetrieve + " @" + RowPICKLIMIT + "=" + this.PICKLIMIT);
      return returnDS;
    }

    public bool RetrieveAll()
    {
      sqlData = new SQLDataObject();
      DataSet resultDS = sqlData.ExecuteDataSetSQL(m_StoredProcedureConstRetrieveall);
      return SetClassPropertiesListFromDataSet(resultDS);
    }

    public bool Create()
    {
      bool success = false;
      string sVariables = "";
      sqlData = new SQLDataObject();

      Type type = this.GetType();
      PropertyInfo[] propInfo = type.GetProperties();

      for (int i = 0; i < propInfo.Length; i++)
      {
        if (propInfo[i].PropertyType.Name.IndexOf("List") == -1)
        {
          sVariables += "@" + propInfo[i].Name + " = ";
          if (propInfo[i].PropertyType.Name == "String")
            sVariables += "'" + propInfo[i].GetValue(this, null) + "', ";
          else
            sVariables += propInfo[i].GetValue(this, null) + ", ";
        }
      }

      success = sqlData.ExecuteNonQuerySQL(m_StoredProcedureConstCreate + " " + sVariables.Substring(0, sVariables.Length - 2));

      return success;
    }

    public bool Update()
    {
      bool success = false;
      string sVariables = "";
      sqlData = new SQLDataObject();

      Type type = this.GetType();
      PropertyInfo[] propInfo = type.GetProperties();

      for (int i = 0; i < propInfo.Length; i++)
      {
        if (propInfo[i].PropertyType.Name.IndexOf("List") == -1)
        {
          sVariables += "@" + propInfo[i].Name + " = ";
          if (propInfo[i].PropertyType.Name == "String")
            sVariables += "'" + propInfo[i].GetValue(this, null) + "', ";
          else
            sVariables += propInfo[i].GetValue(this, null) + ", ";
        }
      }

      success = sqlData.ExecuteNonQuerySQL(m_StoredProcedureConstUpdate + " " + sVariables.Substring(0, sVariables.Length - 2));

      return success;
    }

    private bool SetClassPropertiesFromDataSet(DataSet resultDS)
    {
      bool success = false;
      if (resultDS.Tables[0] != null)
      {
        if (resultDS.Tables[0].Rows.Count > 0)
        {
          foreach (DataRow dr in resultDS.Tables[0].Rows)
          {
					if (dr[RowPICKLIMIT].ToString() != "")
			this.PICKLIMIT = decimal.Parse(dr[RowPICKLIMIT].ToString());

		if (dr[RowMAXDAYS].ToString() != "")
			this.MAXDAYS = decimal.Parse(dr[RowMAXDAYS].ToString());

		if (dr[RowMAXREQ].ToString() != "")
			this.MAXREQ = decimal.Parse(dr[RowMAXREQ].ToString());

		if (dr[RowSENDMAIL].ToString() != "")
			this.SENDMAIL = bool.Parse(dr[RowSENDMAIL].ToString());

		if (dr[RowSMTPIP].ToString() != "")
			this.SMTPIP = dr[RowSMTPIP].ToString();

		if (dr[RowSENDEREMAIL].ToString() != "")
			this.SENDEREMAIL = dr[RowSENDEREMAIL].ToString();

		if (dr[RowSENDERNAME].ToString() != "")
			this.SENDERNAME = dr[RowSENDERNAME].ToString();

		if (dr[RowSENDSTSERR].ToString() != "")
			this.SENDSTSERR = bool.Parse(dr[RowSENDSTSERR].ToString());

		if (dr[RowSENDSTSTO].ToString() != "")
			this.SENDSTSTO = dr[RowSENDSTSTO].ToString();

		if (dr[RowRETWARN].ToString() != "")
			this.RETWARN = bool.Parse(dr[RowRETWARN].ToString());

		if (dr[RowPICKFOLDER].ToString() != "")
			this.PICKFOLDER = decimal.Parse(dr[RowPICKFOLDER].ToString());

		if (dr[RowQCLETBODY].ToString() != "")
			this.QCLETBODY = dr[RowQCLETBODY].ToString();

		if (dr[RowORDERNO].ToString() != "")
			this.ORDERNO = int.Parse(dr[RowORDERNO].ToString());

		if (dr[RowORDERTOEMAIL].ToString() != "")
			this.ORDERTOEMAIL = dr[RowORDERTOEMAIL].ToString();
          }
          success = true;
        }
      }
      return success;
    }

    private bool SetClassPropertiesListFromDataSet(DataSet resultDS)
    {
      bool success = false;
      if (resultDS.Tables[0] != null)
      {
        if (resultDS.Tables[0].Rows.Count > 0)
        {
          foreach (DataRow dr in resultDS.Tables[0].Rows)
          {
            DOTsetup Tsetup = new DOTsetup();
			
					if (dr[RowPICKLIMIT].ToString() != "")
			Tsetup.PICKLIMIT = decimal.Parse(dr[RowPICKLIMIT].ToString());

		if (dr[RowMAXDAYS].ToString() != "")
			Tsetup.MAXDAYS = decimal.Parse(dr[RowMAXDAYS].ToString());

		if (dr[RowMAXREQ].ToString() != "")
			Tsetup.MAXREQ = decimal.Parse(dr[RowMAXREQ].ToString());

		if (dr[RowSENDMAIL].ToString() != "")
			Tsetup.SENDMAIL = bool.Parse(dr[RowSENDMAIL].ToString());

		if (dr[RowSMTPIP].ToString() != "")
			Tsetup.SMTPIP = dr[RowSMTPIP].ToString();

		if (dr[RowSENDEREMAIL].ToString() != "")
			Tsetup.SENDEREMAIL = dr[RowSENDEREMAIL].ToString();

		if (dr[RowSENDERNAME].ToString() != "")
			Tsetup.SENDERNAME = dr[RowSENDERNAME].ToString();

		if (dr[RowSENDSTSERR].ToString() != "")
			Tsetup.SENDSTSERR = bool.Parse(dr[RowSENDSTSERR].ToString());

		if (dr[RowSENDSTSTO].ToString() != "")
			Tsetup.SENDSTSTO = dr[RowSENDSTSTO].ToString();

		if (dr[RowRETWARN].ToString() != "")
			Tsetup.RETWARN = bool.Parse(dr[RowRETWARN].ToString());

		if (dr[RowPICKFOLDER].ToString() != "")
			Tsetup.PICKFOLDER = decimal.Parse(dr[RowPICKFOLDER].ToString());

		if (dr[RowQCLETBODY].ToString() != "")
			Tsetup.QCLETBODY = dr[RowQCLETBODY].ToString();

		if (dr[RowORDERNO].ToString() != "")
			Tsetup.ORDERNO = int.Parse(dr[RowORDERNO].ToString());

		if (dr[RowORDERTOEMAIL].ToString() != "")
			Tsetup.ORDERTOEMAIL = dr[RowORDERTOEMAIL].ToString();

            this.Tsetups.Add(Tsetup);
          }
          success = true;
        }
      }
      return success;
    }
    #endregion

    #region Class Support Methods
    #endregion
  }
}
